﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TerminationCall.Core {

    public class LayerManager {

        private Layer _layer;
    
        public LayerManager(Layer layer) {
            _layer = layer;
        }

        public Field GetField(int x, int y) {

            if (x >= _layer.Fields.GetLength(0)) {
                return null;
            }
            if (y >= _layer.Fields.GetLength(1)) {
                return null;
            }
            if (x < 0) {
                return null;
            }
            if (y < 0) {
                return null;
            }

            return _layer.Fields[x, y];

        }

        public void SetTile(int x, int y, Tile tile, TileFields fieldTile) {

            Field field = GetField(x, y);

            if (field == null) {
                return;
            }

            switch (fieldTile) {

                case TileFields.BackgroundTile:
                    field.BackgroundTile = tile;
                    break;

                case TileFields.ForegroundTile:
                    field.ForegroundTile = tile;
                    break;

                default:
                    field.TileField.SetTile(fieldTile, tile);
                    break;

            }

        }

        public void SetTile(int x, int y, Tile tile) {

            Field field = GetField(x, y);

            if (field == null) {
                return;
            }

            field.TileField.SetTile(tile);
        }

        public void SetField(int x, int y, Field newField) {

            if ( GetField(x, y) == null) {
                return;
            }

            _layer.Fields[x, y] = newField.Clone() as Field;
        }

    }

}
